package com.wind.weblog.admin.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wind.weblog.admin.dao.AdminArticleDao;
import com.wind.weblog.common.domain.mapper.ArticleMapper;
import com.wind.weblog.common.domain.dos.ArticleCountDO;
import com.wind.weblog.common.domain.dos.ArticleDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * @author: chengzi
 * @url: www.flyecho.fun
 * @date: 2024-09-17 12:08
 * @description: TODO
 **/
@Service
@Slf4j
public class AdminArticleDaoImpl implements AdminArticleDao {
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public int insertArticle(ArticleDO articleDO) {
        return articleMapper.insert(articleDO);
    }

    @Override
    public ArticleDO queryByArticleId(Long articleId) {
        QueryWrapper<ArticleDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ArticleDO::getId, articleId).eq(ArticleDO::getIsDeleted, 0);
        return articleMapper.selectOne(wrapper);
    }

    @Override
    public Page<ArticleDO> queryArticlePageList(Long current, Long size, Date startDate, Date endDate, String searchTitle) {
        // 默认日期范围
        LocalDate defaultStartDate = LocalDate.of(2000, 1, 1);
        LocalDate defaultEndDate = LocalDate.of(2999, 12, 31);

        LocalDate startLocalDate = (startDate != null) ? startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate() : defaultStartDate;
        LocalDate endLocalDate = (endDate != null) ? endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate() : defaultEndDate;
        Page<ArticleDO> articlePage = articleMapper.selectPageList(current, size, searchTitle, startLocalDate, endLocalDate);

        for (ArticleDO articleDO : articlePage.getRecords()) {
            articleDO.setIsTop(articleDO.getWeight() > 0);
        }

        return articlePage;
    }

    @Override
    public int deleteById(Long articleId) {
        return articleMapper.deleteById(articleId);
    }

    @Override
    public int updateById(ArticleDO articleDO) {
        return articleMapper.updateById(articleDO);
    }

    @Override
    public Long selectTotalCount() {
        QueryWrapper<ArticleDO> wrapper = new QueryWrapper<>();
        wrapper.select("1").lambda().eq(ArticleDO::getIsDeleted, 0);
        return articleMapper.selectCount(wrapper);
    }

    @Override
    public List<ArticleCountDO> selectArticleCount(String startDate, String endDate) {
        return articleMapper.selectArticleCount(startDate, endDate);
    }

    @Override
    public int readNumIncrease(Long articleId) {
        UpdateWrapper<ArticleDO> wrapper = new UpdateWrapper<>();
        wrapper.lambda().setSql("read_num = read_num + 1").eq(ArticleDO::getId, articleId);
        return articleMapper.update(null, wrapper);
    }
}
